package aes.gui;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import aes.general.AdminControl;

/**
 * The Class AdminTeacherStatsPanel. This panel provides statistical information
 * about spesific teacher for the admin's reports.
 */
public class AdminTeacherStatsPanel extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jp;
	private JLabel lblTeachersGrades;
	private JSeparator separator;
	private static ArrayList<String> ExamId;
	private JButton btnPrev;
	private JTextField avgtext;
	private JTextField medtext;
	private JTextField numtext;
	private JComboBox tidBox;
	private ArrayList<Double> stats;
	private Vector<Vector<String>> Teachersvec ;
	private JTextField techtext;
	private DefaultTableModel model ;
	private JTable table;
	private JComboBox exambox;
	
	

	/**
	 * Instantiates a new admin teacher stats panel.
	 * 
	 * @param jp
	 *            the jp
	 */
	public AdminTeacherStatsPanel(JPanel jp) {
		this.jp = jp;
		initialize();
		
	}
	
	/**
	 * Initialize.
	 */
	private void initialize() {
		setLayout(null);
		lblTeachersGrades = new JLabel("Teacher Statistics");
		lblTeachersGrades.setFont(new Font("Tempus Sans ITC", Font.BOLD, 21));
		lblTeachersGrades.setBounds(238, 11, 164, 29);
		add(lblTeachersGrades);
		
		separator = new JSeparator();
		separator.setBounds(10, 43, 620, 2);
		add(separator);
		
		btnPrev = new JButton("Back");
		btnPrev.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AdminControl.setUserInfo(null);
				CardLayout cl = (CardLayout) (jp.getLayout());
				cl.show(jp, "reports");
			}
		});
		btnPrev.setBounds(189, 374, 107, 23);
		add(btnPrev);
	          
		JLabel lblStudents = new JLabel("Teacher Name");
		lblStudents.setBounds(238, 56, 107, 14);
		add(lblStudents);
		
		JLabel lblA = new JLabel("Average");
		lblA.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblA.setBounds(50, 140, 69, 23);
		add(lblA);
		
		avgtext = new JTextField();
		avgtext.setEditable(false);
		avgtext.setFont(new Font("Tahoma", Font.BOLD, 14));
		avgtext.setBounds(150, 141, 47, 20);
		add(avgtext);
		avgtext.setColumns(10);
		
		JLabel lblMedian = new JLabel("Median");
		lblMedian.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblMedian.setBounds(49, 187, 69, 23);
		add(lblMedian);
		
		medtext = new JTextField();
		medtext.setFont(new Font("Tahoma", Font.BOLD, 14));
		medtext.setEditable(false);
		medtext.setBounds(150, 190, 47, 20);
		add(medtext);
		medtext.setColumns(10);
		
		JLabel lblDistribution = new JLabel("Distribution");
		lblDistribution.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblDistribution.setBounds(49, 241, 86, 23);
		add(lblDistribution);
		
		JPanel Tablepanel = new JPanel();
		Tablepanel.setBounds(49, 275, 454, 76);
		add(Tablepanel);
		
		Tablepanel.setLayout(null);
		
		String col[] = {"1","2","3","4","5","6","7","8","9","10"};
		String data[][] = {{}};
		model = new DefaultTableModel(data,col);
		
		
		table = new JTable(model);
		table.setBackground(new Color(245, 255, 250));
		table.setFont(new Font("Tahoma", Font.PLAIN, 12));
		table.setBounds(10, 11, 423, 36);
		Tablepanel.add(table);
		int j = 0;
		model.insertRow(j,new Object[]{"0-9","10-19","20-29","30-39","40-49","50-59","60-69","70-79"
				,"80-89","90-100"});
		
		JLabel lblNumberOfExams = new JLabel("Number of exams : ");
		lblNumberOfExams.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblNumberOfExams.setBounds(270, 146, 162, 29);
		add(lblNumberOfExams);
		
		techtext = new JTextField();
		techtext.setEditable(false);
		techtext.setBounds(330, 53, 86, 20);
		add(techtext);

		numtext = new JTextField();
		numtext.setFont(new Font("Tahoma", Font.BOLD, 14));
		numtext.setEditable(false);
		numtext.setBounds(442, 152, 32, 20);
		add(numtext);
		numtext.setColumns(10);
		
		while((Teachersvec = AdminControl.getUserInfo())== null);
		tidBox = new JComboBox();
		tidBox.setBounds(49, 98, 60, 20);
		add(tidBox);
		tidBox.addItem("-----");
		for(int i =0 ;i<Teachersvec.size();i++)
			tidBox.addItem(Teachersvec.get(i).get(0));
		tidBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {	
				//AdminControl.setExamID(null);
				int index = tidBox.getSelectedIndex(); 
				if(tidBox.getSelectedIndex() == 0){
					techtext.setText(null);
					exambox.removeAllItems();
					exambox.addItem("------");
					
				}
				else
				{
					techtext.setText(Teachersvec.get(index-1).get(1)+ " " + Teachersvec.get(index-1).get(2));
					AdminControl.getTeacherExamInfo((String) tidBox.getSelectedItem());
					while((ExamId = AdminControl.getExamID()) == null);
					exambox.removeAllItems();
					exambox.addItem("------");
					if(ExamId.get(0).equals("error"))
					{
						JOptionPane.showMessageDialog(null, "Error,No Exams For This Techer", "Error",
								JOptionPane.ERROR_MESSAGE);
					}
					else{
					for(String EId:ExamId)
						exambox.addItem(EId);
					}
					AdminControl.setExamID(null);		
					
				}
			}
		});	
		
		
		exambox = new JComboBox();
		exambox.setBounds(141, 100, 60, 20);
		add(exambox);
		exambox.addItem("------");
		
		JLabel lblTeacerId = new JLabel("Teacer Id");
		lblTeacerId.setBounds(52, 78, 67, 14);
		add(lblTeacerId);
		
		JLabel lblExamId = new JLabel("Exam Id");
		lblExamId.setBounds(151, 78, 46, 14);
		add(lblExamId);
		
		
		exambox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {	
				int index = tidBox.getSelectedIndex();
				if(exambox.getSelectedIndex() == 0){
					avgtext.setText(null);
					medtext.setText(null);
					numtext.setText(null);
					model.insertRow(1,new Object[]{null,null,null,null,null,null,null,null,null,null});
					model.removeRow(2);
				}
				else {
					if((String) exambox.getSelectedItem() != null){
					AdminControl.getTeacerExamStat((String) exambox.getSelectedItem());
					while(AdminControl.getGrades()==null);	
					stats = AdminControl.CulcStats(AdminControl.getGrades()); 
					avgtext.setText(stats.get(0).toString());
					medtext.setText(stats.get(1).toString());
					numtext.setText(stats.get(2).toString());
					int[] dis = AdminControl.getDistribution(AdminControl.getGrades());
					model.insertRow(1,new Object[]{dis[0],dis[1],dis[2],dis[3],dis[4],dis[5],dis[6],dis[7],dis[8],dis[9]});
					model.removeRow(2);
					AdminControl.setGrades(null);
					}
				}
			}
		});	
		
	}
}

	



